//
//  ChangePhoneTwo.swift
//  LeGou
//
//  Created by lxm on 2022/8/3.
//

import UIKit

class TradeFroget: ZMViewController {

    //MARK:--------------------属性--------------------
    // 0 交易密码 1 绑定银行卡
    var type = 0
    
    var phoneStr : String?
    
    var pa : [String : Any]?
    //MARK:--------------------UI控件--------------------
    @IBOutlet weak var phone: UITextField!
    
    @IBOutlet weak var code: UITextField!
    
    @IBOutlet weak var nextButton: UIButton!
    
    @IBOutlet weak var tips: UILabel!
    
    //MARK:--------------------生命周期--------------------
    override func viewDidLoad() {
        super.viewDidLoad()
        setUI()
        addConstraints()
        bindAction()
    }
    
    //MARK:--------------------UI--------------------
    func setUI() {
        phone.text = ZMAppShareData.share.safeUserInfo.phone.zm.hidPhone()
        phone.isUserInteractionEnabled = false
        if type == 0 {
            title = "设置支付密码"
            tips.text = "请输入验证码"
        } else {
            if let phoneStr = phoneStr {
                self.phone.text = phoneStr
            }
            title = "安全验证"
            tips.text = "验证手机号"
        }
        
    }
    
    func addConstraints() {
        
    }
    
    //MARK:--------------------methods--------------------
    func bindAction() {
        code.rx.text.orEmpty.map({$0.count == 6}).subscribe(onNext:{ [unowned self] resp in
            self.nextButton.isEnabled = resp
            self.nextButton.backgroundColor = resp ? Style.mainColor : Style.disableColor
        }).disposed(by: disposeBag)
    }
    
    @IBAction func onCode(_ sender: CountButton) {
        
        LoginService.shared.showVerifycation(vc: self) { success in
            if success {
                let phone = self.type == 0 ? ZMAppShareData.share.safeUserInfo.phone : self.phoneStr!
                LoginAPI.sendCode(["phone" : phone]).rxRequestNullAble(type: Any.self).subscribe(onNext:{ [unowned self] resp in
                    sender.start(time: 60)
                }).disposed(by: self.disposeBag)
            }
        }
    }
    

    @IBAction func onNext(_ sender: Any) {
        if type == 0 {
            let vc = TradePwdSet()
            vc.type = 1
            vc.pa["captcha"] = code.text!
            vc.pa["phone"] = ZMAppShareData.share.safeUserInfo.phone
            self.navigationController?.pushViewController(vc, animated: true)
        } else {
            self.bankVerify(code: code.text!)
        }
    }
    
    func bankVerify(code : String) {
        guard var pa = pa else {return}
        pa["captcha"] = code
        ZMToast.showActivity()
        MineAPI.bindBankCard(pa).rxRequest(type: Bool.self).subscribe(onNext:{ [unowned self] resp in
            if resp {
                ZMToast.hiddenActivity()
                _ = ZMJumpHelper.jumpToClass(VC: self, toClass: ThirdAccountVC.classForCoder(), notContainAutoJump: true)
                ZMAppShareData.share.requestUserInfo { ok in
                    
                }
                ZMNotification.shard.subject.onNext(.bindThirdSuccess)
            } else {
                ZMToast.showToast(title: "校验出错")
            }
        }).disposed(by: disposeBag)
    }
    //MARK:--------------------delegate--------------------

}
